/*
Automatic clinic tests using a robot arm.
Copyright (C) 2010  CIC-CPC, CHU Gabriel Montpied
All other trademarks, logos and copyrights are the property
of their respective owners.

This file is part of cpc-painbot.

cpc-painbot is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

cpc-painbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with cpc-painbot.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "testvonfrey.h"

#include <QTest>

#include "bras.h"
#include "filament.h"
#include "stimulation/vonfrey.h"

void TestVonFrey::initTestCase()
{

// Initialisation du Bras.
    bras_ = new Bras(this);
    bras_->setPort("COM6");

    bras_->connecter();

    // Attendre que le bras réponde.
    while ( bras_->etat() != Bras::EtatNonPret )
        QTest::qWait(250);

    bras_->initialiser();

    // Attendre que le bras soit initialisé.
    while ( bras_->etat() != Bras::EtatPret )
        QTest::qWait(10000);

// Filament factice.
    filament_ = new Filament(this);
    filament_->setPosition(PositionBras(-170, 390));

// Le Von Frey.
    vonfrey_ = new VonFrey(this);
    vonfrey_->setBras(bras_);
    vonfrey_->setFilament(filament_);
    vonfrey_->setDuree(3000);
}

#if 0
void TestVonFrey::testPrendreFilament()
{
    vonfrey_->prendreFilament(filament_);

    while ( bras_->etat() != Bras::EtatPret )
        QTest::qWait(5000);

    // Dégage le bras.
    PositionBras pos = bras_->position();

    pos.setX(-100);

    bras_->setPosition(pos, 50);

    while ( bras_->etat() != Bras::EtatPret )
        QTest::qWait(5000);
}

void TestVonFrey::testRendreFilament()
{
    vonfrey_->rendreFilament(filament_);

    while ( bras_->etat() != Bras::EtatPret )
        QTest::qWait(5000);
}
#endif

void TestVonFrey::testStimuler()
{
    while ( bras_->etat() != Bras::EtatPret )
        QTest::qWait(250);

    vonfrey_->setFilament(filament_);
    vonfrey_->setPosition(PositionBras(0, 190));
    vonfrey_->stimuler();
    QCoreApplication::processEvents();

    qDebug() << "isRunning (pre):" << vonfrey_->isRunning();

    do {
        QCoreApplication::processEvents();
        QTest::qSleep(10);
    } while ( vonfrey_->isRunning() );

    qDebug() << "isRunning (post):" << vonfrey_->isRunning();

    qDebug() << "Erreur :" << vonfrey_->errorString();
}
